package handledb;

public class TransactionItem {
    private static long index=0;
    public long time;
    public long effectiveTime;
    private Pipeline[] pipelines = new Pipeline[3];
    private int pipelineSize = 0;
    public long threadId;
    public long transactionId;
    private String[] urls = new String[3];
    private int urlSize = 0;
    public boolean isLocal = true;

    public TransactionItem(long threadId){
        this.threadId = threadId;
        transactionId = System.currentTimeMillis()*1000+index+threadId*1000000;
        index = (index+1)%1000;
        time = System.currentTimeMillis();
    }
    public TransactionItem(long threadId, Long transactionId){
        this.threadId = threadId;
        if(transactionId==null){
            this.transactionId = System.currentTimeMillis()*1000+index+threadId*1000000;
            index = (index+1)%1000;
        }else{
            this.transactionId = transactionId;
        }
        time = System.currentTimeMillis();
    }

    public void putPipeline(Pipeline pipeline){
        if(pipelineSize==pipelines.length){
            Pipeline[] arr = new Pipeline[pipelines.length+5];
            for(int i=0;i<pipelineSize;i++){
                arr[i] = pipelines[i];
            }
        }
        pipelines[pipelineSize] = pipeline;
        pipelineSize += 1;
    }

    public Pipeline[] getPipelines(){
        Pipeline[] arr = new Pipeline[pipelineSize];
        for(int i=0;i<pipelineSize;i++){
            arr[i] = pipelines[i];
        }
        return arr;
    }

    public void putUrl(String url){
        boolean next = true;
        for (int i = 0; i < urlSize; i++) {
            if(url.equals(urls[i])){
                next = false;
                break;
            }
        }
        if(next){
            if(urlSize==urls.length){
                String[] arr = new String[urlSize+5];
                for (int i = 0; i < urlSize; i++) {
                    arr[i] = urls[i];
                }
            }
            urls[urlSize] = url;
            urlSize += 1;
        }
    }

    public String[] getUrls(){
        String[] arr = new String[urlSize];
        for(int i=0;i<urlSize;i++){
            arr[i] = urls[i];
        }
        return arr;
    }
}
